package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PhysicalExamDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.myExceptions.AssociatedException;
import com.itheima.pojo.PhysicalExam;
import com.itheima.service.PhysicalExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 体检结果服务实现类
 */
@Service(interfaceClass = PhysicalExamService.class)
@Transactional
public class PhysicalExamServiceImpl implements PhysicalExamService {

    @Autowired
    private PhysicalExamDao physicalExamDao;

    //生成体检结果
    @Override
    public void add(PhysicalExam physicalExam) throws AssociatedException {
        Integer orderId = physicalExam.getOrderId();
        //根据ID查询数量
        long count = physicalExamDao.findCountById(orderId);
        if (count > 0) {
            throw new AssociatedException();
        }
        //调用DAO
        physicalExamDao.add(physicalExam);
    }

    //分页查询体检结果
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //获取分页参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用分页助手
        PageHelper.startPage(currentPage, pageSize);
        //根据条件查询
        Page<Map<String,Object>> page = physicalExamDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据预约订单id查询体检结果
    @Override
    public Map findByOrderId(Integer orderId) {
        Map map = physicalExamDao.findByOrderId(orderId);
        return map;
    }
}
